package org.flik.client.shared;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootLayoutPanel;

public class ClientUtils {
	
	public static DateTimeFormat DTF_GENERAL_DISPLAY = DateTimeFormat.getFormat("MM/dd/yyyy hh:mma");
	
	public static boolean isBlank(String text){
		return text.trim().equals("");
	}
	public static boolean isBlank(HasValue<String> valueWidget) {
		return isBlank(valueWidget.getValue());
	}
	public static <T> boolean isEqual(HasValue<T> valueWidget1, HasValue<T> valueWidget2) {
		return valueWidget1 != null && valueWidget2 != null && valueWidget1.getValue().equals(valueWidget2.getValue());
	}
	public static boolean isValidEmail(String email) {
		return isBlank(email);
	}
	public static boolean isValidEmail(HasValue<String> emailWidget) {
		return isBlank(emailWidget);
	}

	private static final int REMOTE_COMMUNICATION_MESSAGE_WIDTH_PX = 120;
	private static final Label remoteCommunicationMessage = new Label();
	public static void showRemoteCommunicationMessage(String text){
		RootLayoutPanel rootLayoutPanel = RootLayoutPanel.get(); 
		remoteCommunicationMessage.setText(text);
		rootLayoutPanel.add(remoteCommunicationMessage);
		rootLayoutPanel.setWidgetTopHeight(remoteCommunicationMessage, 0, Unit.PX, 25, Unit.PX);
		rootLayoutPanel.setWidgetLeftWidth(remoteCommunicationMessage, (Window.getClientWidth() - REMOTE_COMMUNICATION_MESSAGE_WIDTH_PX)/2.0, Unit.PX, REMOTE_COMMUNICATION_MESSAGE_WIDTH_PX, Unit.PX);
	}
	public static void hideRemoteCommunicationMessage(){//TODO: stack and track progress of multiple parallel remoteCommunicationMessages
		RootLayoutPanel.get().remove(remoteCommunicationMessage);
	}
	public static void selectListBoxItem(ListBox listBox, Object value) {
		
		if (value == null){
			listBox.setSelectedIndex(0);
			return;
		}
		int itemCount = listBox.getItemCount(); 
		String item;
		for (int i=0; i<itemCount; i++){
			item = listBox.getValue(i);
			if (item != null){
				System.out.println(value + "-" + item + " " + listBox.getItemText(i));
				if (value.toString().equals(item)){
					listBox.setSelectedIndex(i);
				}
			}
		}
	}
	
}
